<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>闭包</title>
</head>
<body>
    <script>
        // 子函数可以访问到其他函数里的特性，称之为闭包
        function test(){
            let a = 1;
            function show(){
                console.log(a);
            };
            show();
        };
        test();



        // 获取数组中想要的值
        let arr = [1,2,34,5,6,7,8,9];

        // 不使用闭包
        // let w = arr.filter(value => {
        //     return value >= 3 && value <= 9;
        // });
        // console.log(w);

        // 使用闭包
        function between(a,b){
            return function(value){
                return value >= a && value <= b;
            }
        };
        let g = arr.filter(between(5,8));
        console.log(g);



        // 获取价格区间内的商品
        let lessons = [
            {
                name : 'html',
                price : 99
            },
            {
                name : 'css',
                price : 89
            },
            {
                name : 'js',
                price : 109
            }
        ];
        function space(a,b){
            return function(value){
                return value.price >= a && value.price <= b;
            }
        };
        let les = lessons.filter(space(90,100));
        console.table(les);
    </script>
</body>
</html>